﻿/*

******************************************************

CreateDate  *  Author         *  Content

2008-08-05   *  donne.yang     *  Main Function

******************************************************

NO   *  UpdateDate   *  Author        *  Content

*********************************************************

*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Odbc;
using System.Threading;

using FoxConn.Lib.Dbo;

namespace OracleToInformix.Source
{
    class MainFunction
    {
        Thread threadHrmEmployees,threadClose;
        bool blnHrmEmployees,blnDeleteOldData;

        public MainFunction()
        { 
        }

        #region Threads Operation
        /// <summary>
        ///开启数据导入线程
        /// </summary>
        /// 
        public void OracleToInformix()
        {
            DBException dbex = new DBException();

            dbex.SaveErrorLog(DateTime.Now + ":Job Starting...");
            try
            {
                if (threadHrmEmployees == null)
                {
                    this.HrmEmployeesToOracleStart();
                }
                if (threadClose == null)
                {
                    this.CloseThreadsStart();
                }

                dbex.SaveErrorLog(DateTime.Now + ":Job Started");
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "RunCCEToOracle Error.");
            }
            finally
            { }
        }

        /// <summary>
        /// 检查并关闭线程
        /// </summary>
        private void CloseThreads()
        {

            DBException dbex = new DBException();

            try
            {
                int i = 0;

                while (!blnHrmEmployees)
                {
                    i++;
                    if (blnHrmEmployees)
                    {
                        if (threadHrmEmployees.ThreadState == ThreadState.Running || threadHrmEmployees.ThreadState == ThreadState.Suspended || threadHrmEmployees.ThreadState == ThreadState.WaitSleepJoin)
                        {
                            threadHrmEmployees.Abort();
                            threadHrmEmployees.Join();
                            Console.WriteLine("threadHrmEmployees:" + threadHrmEmployees.ThreadState);
                        }
                    }

                    if (blnDeleteOldData)
                    {
                        Console.WriteLine("TotalCount:" + InformixUtil.GetHrmEmployeeCount() + "      threadHrmEmployees:" + threadHrmEmployees.ThreadState);
                        Console.WriteLine("***************************************************");

                        if (Config.DEBUG.Equals("1"))
                        {
                            if (i % 10 == 0)
                            {
                                Console.Clear();
                            }
                        }
                    }

                    Thread.Sleep(10000);
                }

                if (blnHrmEmployees)
                {
                    dbex.SaveErrorLog(DateTime.Now + ":Job Finished.");
                }
            }
            catch (ThreadAbortException threadEx)
            {
                dbex.SaveException(threadEx, "CloseThreads Thread Abort Error.");
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "CloseThreads Error.");
            }
            finally
            { }
        }

        #endregion

        #region Start Threads
        private void HrmEmployeesToOracleStart()
        {
            DBException dbex = new DBException();

            try
            {
                threadHrmEmployees = new Thread(new ThreadStart(this.HrmEmployeesToOracle));
                threadHrmEmployees.Start();
                Console.WriteLine("threadHrmEmployees start...");
                //dbex.SaveErrorLog(DateTime.Now + "threadInteractionSummary start...");
            }
            catch (ThreadStartException threadEx)
            {
                dbex.SaveException(threadEx, "threadHrmEmployees Error.");
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "threadHrmEmployees Error.");
            }
            finally
            { }
        }

        private void CloseThreadsStart()
        {
            DBException dbex = new DBException();

            try
            {
                threadClose = new Thread(new ThreadStart(CloseThreads));
                threadClose.Start();
            }
            catch (ThreadStartException threadEx)
            {
                dbex.SaveException(threadEx, "CloseThreadsStart Error.");
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "CloseThreadsStart Error.");
            }
            finally
            { }
        }

        #endregion

        #region Interaction Operation
        public void HrmEmployeesToOracle()
        {
            DBException dbex = new DBException();

            try
            {
                blnDeleteOldData = InformixUtil.DeleteOldHrData();

                if (blnDeleteOldData)
                {
                    Oracle2Informix oracle2Informix = new Oracle2Informix();
                    SortedList sd = oracle2Informix.GetColumnsInfo("ecpf_file");


                    DataSet ds = OracleUtil.GetHrmEmployees();

                    if (StaticUtil.IsDsHasData(ds))
                    {
                        foreach (DataRow Row in ds.Tables[0].Rows)
                        {
                            string strSQL = oracle2Informix.InitSQLScript(sd, "ecpf_file");
                            OdbcParameter[] Parameters = oracle2Informix.InitOracleParameter(sd);

                            int i = 0;
                            //IDictionaryEnumerator ide = sd.GetEnumerator();



                            foreach (DictionaryEntry de in sd)
                            {
                                if (Row[de.Key.ToString()] is DBNull || Row[de.Key.ToString()] == null)
                                {
                                    Parameters[i].Value = DBNull.Value;
                                }
                                else
                                {
                                    switch (de.Value.ToString().ToUpper())
                                    {
                                        case "0": Parameters[i].Value = Convert.ToChar(Row[de.Key.ToString()]); break;
                                        case "1": Parameters[i].Value = Convert.ToInt16(Row[de.Key.ToString()]); break;
                                        case "2": Parameters[i].Value = Convert.ToInt32(Row[de.Key.ToString()]); break;
                                        case "4": Parameters[i].Value = Convert.ToDouble(Row[de.Key.ToString()]); break;
                                        case "5": Parameters[i].Value = Convert.ToDecimal(Row[de.Key.ToString()]); break;
                                        case "7": Parameters[i].Value = Convert.ToDateTime(Row[de.Key.ToString()]).ToShortDateString(); break;
                                        case "10": Parameters[i].Value = Convert.ToDateTime(Row[de.Key.ToString()]); break;
                                        case "13": Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;
                                        case "15": Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;
                                        case "16": Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;


                                        //not null
                                        case "256": Parameters[i].Value = Convert.ToChar(Row[de.Key.ToString()]); break;
                                        case "257": Parameters[i].Value = Convert.ToInt16(Row[de.Key.ToString()]); break;
                                        case "258": Parameters[i].Value = Convert.ToInt32(Row[de.Key.ToString()]); break;
                                        case "260": Parameters[i].Value = Convert.ToDouble(Row[de.Key.ToString()]); break;
                                        case "261": Parameters[i].Value = Convert.ToDecimal(Row[de.Key.ToString()]); break;
                                        case "263": Parameters[i].Value = Convert.ToDateTime(Row[de.Key.ToString()]).ToShortDateString(); break;
                                        case "266": Parameters[i].Value = Convert.ToDateTime(Row[de.Key.ToString()]); break;
                                        case "269": Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;
                                        case "271": Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;
                                        case "272": Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;

                                        default: Parameters[i].Value = Row[de.Key.ToString()].ToString(); break;
                                    }
                                }

                                i++;
                            }

                            OdbcDBOpration odbcDBOpration = new OdbcDBOpration();
                            odbcDBOpration.UpdateData(strSQL, Parameters);
                        }
                        this.blnHrmEmployees = true;
                    }
                }
                else
                {
                    Console.WriteLine("delete old data failed.");
                    dbex.SaveErrorLog("delete old data failed.");
                }
            }
            catch (Exception ex)
            {
                dbex.SaveException(ex, "HrmEmployeesToOracle Error.");
                dbex.SaveErrorLog(ex.Source);
                this.blnHrmEmployees = false;
            }
            finally
            {
            }
        }
        #endregion
    }
}
